package com.acwing.partition7;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author rkc
 * @date 2022/7/16 10:22
 */
public class AC693行程排序 {

    private static final int N = 10010;
    private static Map<String, Integer> din = new HashMap<>(N);
    private static Map<String, String> map = new HashMap<>(N);
    private static int n;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int t = nextInt();
        for (int k = 1; k <= t; k++) {
            n = nextInt();
            for (int i = 0; i < n; i++) {
                String src = next(), dest = next();
                map.put(src, dest);
                din.put(dest, din.getOrDefault(dest, 0) + 1);
                if (din.containsKey(src)) continue;
                din.put(src, 0);
            }
            writer.format("Case #%d: ", k);
            resolve();
        }
        writer.flush();
    }

    private static void resolve() {
        //找到入度为0的点
        String cur = null;
        for (Map.Entry<String, Integer> entry : din.entrySet()) {
            if (entry.getValue() == 0) {
                cur = entry.getKey();
                break;
            }
        }
        //从cur开始走
        while (map.containsKey(cur)) {
            String ne = map.get(cur);
            writer.format("%s-%s ", cur, ne);
            cur = ne;
        }
        writer.write("\n");
        din.clear();
        map.clear();
    }

    private static String next() throws IOException {
        tokenizer.nextToken();
        return tokenizer.sval;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
